package com.smec.remes.module.drafts.adapter;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.SmecRxBus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutRecyclerviewDraftBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.Judge;
import com.smec.remes.bean.MatnrList;
import com.smec.remes.bean.RequestSearch;
import com.smec.remes.module.request.SmecRequestFragment;
import com.smec.remes.module.submit.SmecSubmitFragment;
import com.smec.remes.util.CommonUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import static com.smec.remes.module.request.SmecRequestFragment.DraftsActivity;

/**
 * Created by Administrator on 2017/12/7.
 */

public class SmecDraftsAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {

    private Context mcontext;
    private Boolean vis=false;
    private Boolean src=false;
    private String edit="";
    private boolean _intent=true;
    private boolean firstAll=false;
    private HashMap<String,Boolean> hashMap=new HashMap<>();
    private Judge judge;
    protected Bus mRxbus;
    private int num=0;
    private ArrayList<RequestSearch> arrayList=new ArrayList<>();
    public SmecDraftsAdapter(Context context) {
        this.mcontext=context;
        mRxbus= SmecRxBus.get();
        mRxbus.register(this);
    }

    public void set_intent(boolean a){
        _intent=a;
        notifyDataSetChanged();
    }

    public void _firstAll(boolean a){
        firstAll=a;
        notifyDataSetChanged();
    }
    public void setArrayList(ArrayList<RequestSearch> list){
        arrayList=list;
        notifyDataSetChanged();
    }

    public void clearList(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    public HashMap<String,Boolean> getHashmap(){
        return hashMap;
    }

    public void setHashMap(){
        for(int i=0;i<arrayList.size();i++){
            hashMap.put(arrayList.get(i).getApplyOrderNo(),false);
        }
    }

    public void setHashMapStatus(boolean status){
        edit="edit";
        for(int i=0;i<arrayList.size();i++){
            hashMap.put(arrayList.get(i).getApplyOrderNo(),status);
        }
        notifyDataSetChanged();
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutRecyclerviewDraftBinding layoutRecyclerviewDraftBinding= DataBindingUtil.inflate
                (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_draft,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutRecyclerviewDraftBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewDraftBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        final LayoutRecyclerviewDraftBinding layoutRecyclerviewDraftBinding= (LayoutRecyclerviewDraftBinding) holder.getBinding();
        judge=new Judge();
        judge.setDiaplay(vis);
        judge.setSrc(src);
        layoutRecyclerviewDraftBinding.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(hashMap.get(arrayList.get(position).getApplyOrderNo())){
                    layoutRecyclerviewDraftBinding.ivCheck.setImageResource(R.drawable.smec_nogou);
                    hashMap.put(arrayList.get(position).getApplyOrderNo(),false);
                }else {
                    layoutRecyclerviewDraftBinding.ivCheck.setImageResource(R.drawable.yes_shouhuo);
                    hashMap.put(arrayList.get(position).getApplyOrderNo(),true);
                }
            }
        });
        if(edit.equals("edit")){
            if(hashMap.get(arrayList.get(position).getApplyOrderNo())){
                layoutRecyclerviewDraftBinding.ivCheck.setImageResource(R.drawable.yes_shouhuo);
            }else {
                layoutRecyclerviewDraftBinding.ivCheck.setImageResource(R.drawable.smec_nogou);
            }
        }
        if(arrayList.get(position).getPurpose().equals("SALE")){
            layoutRecyclerviewDraftBinding.tvSale.setText("拟销售");
        }else if(arrayList.get(position).getPurpose().equals("RECEIVE")){
            layoutRecyclerviewDraftBinding.tvSale.setText("拟领用");
        }else {
            layoutRecyclerviewDraftBinding.tvSale.setText("拟三包");
        }
        if(firstAll){
            layoutRecyclerviewDraftBinding.ivCheck.setImageResource(R.drawable.smec_nogou);
            firstAll=false;
        }
        if(!TextUtils.isEmpty(arrayList.get(position).getCustomerName())
                &&!TextUtils.isEmpty(arrayList.get(position).getPurposeName())&&!TextUtils.isEmpty(arrayList.get(position).getDeliveryDate())){
            layoutRecyclerviewDraftBinding.tvImperfect.setVisibility(View.GONE);
        }else {
            layoutRecyclerviewDraftBinding.tvImperfect.setVisibility(View.VISIBLE);
        }
        layoutRecyclerviewDraftBinding.llDrafts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if(_intent){
                   RequestSearch requestSearch=new RequestSearch();
                   requestSearch.setCustomerName(_setString(arrayList.get(position).getCustomerName()));
                   requestSearch.setPurposeName(_setString(arrayList.get(position).getPurposeName()));
                   requestSearch.setDeliveryDate(_setString(arrayList.get(position).getDeliveryDate()));
                   requestSearch.setApplyOrderNo(_setString(arrayList.get(position).getApplyOrderNo()));
                   mRxbus.post("draftsOnclick",requestSearch);
               }else {
                   if(hashMap.get(arrayList.get(position).getApplyOrderNo())){
                       layoutRecyclerviewDraftBinding.ivCheck.setImageResource(R.drawable.smec_nogou);
                       hashMap.put(arrayList.get(position).getApplyOrderNo(),false);
                   }else {
                       layoutRecyclerviewDraftBinding.ivCheck.setImageResource(R.drawable.yes_shouhuo);
                       hashMap.put(arrayList.get(position).getApplyOrderNo(),true);
                   }
               }
            }
        });

        layoutRecyclerviewDraftBinding.setJudge(judge);
        layoutRecyclerviewDraftBinding.setVariable(BR.request,arrayList.get(position));
        num =0;
        holder.getBinding().executePendingBindings();
    }


    public String _setString(String a){
        String str="";
        if(!TextUtils.isEmpty(a)){
            str=a;
        }
        return  str;
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void setVis(boolean a){
        vis=a;
        notifyDataSetChanged();
    }

    public void setSrc(boolean a){
        src=a;
        notifyDataSetChanged();
    }
}
